package net.meblabs.commons.progress;

class ProgressImpl implements Progress {

	private Long id;
	private ProgressState state;
	private boolean initialized;

	ProgressImpl(Long id) {
		this.id = id;
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public ProgressState getState() {
		checkInitialized();
		return state;
	}

	@Override
	public void advance(String message) {
		checkInitialized();
		state.setDone(state.getDone() + 1);
		state.setMessage(message);
	}

	@Override
	public void initialize(String message) {
		initialize(ProgressState.UNKNOWN, message);
	}

	@Override
	public void initialize(int total, String message) {
		if (!initialized) {
			state = new ProgressState(total, message);
			initialized = true;
		}
	}

	@Override
	public void setTotal(int total) {
		checkInitialized();
		state.setTotal(total);
	}

	private void checkInitialized() {
		if (!initialized) {
			throw new IllegalStateException("Not initialized");
		}
	}

	@Override
	public void setCompleted() {
		state.setTotal(state.getDone());
	}
}
